package priv.hxm.view;

import java.awt.Cursor;
import java.awt.EventQueue;
import java.awt.Image;
import java.awt.Insets;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import org.apache.log4j.Logger;

import priv.hxm.clien.Client;
import priv.hxm.speakandlisten.Speak;
import priv.mill.net.MessageContexts;
import priv.mill.net.MessageObj;
import priv.mill.net.MyHttp;

import javax.swing.JLabel;
import javax.swing.ImageIcon;

import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.Toolkit;

public class MainView extends JFrame {
	private static Logger logger = Logger.getLogger(MainView.class);
	private JPanel contentPane;
	private JTextField sendMsg;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainView frame = new MainView();
					frame.setVisible(true);
					new Thread(() -> {// 开启客户端
								new Client().connet("127.0.0.1", 9999);
							}).start();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MainView() {
		setIconImage(Toolkit.getDefaultToolkit().getImage("res/500234.png"));
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 717, 573);
		contentPane = new JPanel();
		contentPane.setBackground(new Color(245, 245, 220));
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JLayeredPane layeredPane = new JLayeredPane();
		layeredPane.setBackground(new Color(245, 245, 220));
		layeredPane.setBounds(14, 13, 671, 500);
		contentPane.add(layeredPane);
		
		JTextArea showMsg = new JTextArea();
		showMsg.setEditable(false);
		showMsg.setFont(new Font("幼圆", Font.BOLD, 15));
		showMsg.setBackground(new Color(255, 250, 205));
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBackground(new Color(222, 184, 135));
		layeredPane.setLayer(scrollPane, 1);
		scrollPane.setBounds(427, 34, 230, 400);
		layeredPane.add(scrollPane);
		showMsg.setLineWrap(true);
		scrollPane.setViewportView(showMsg);
		showMsg.setOpaque(false);

		JPanel panel = new JPanel();
		panel.setBackground(new Color(245, 245, 220));
		layeredPane.setLayer(panel, 10);
		panel.setBounds(415, 13, 242, 432);
		layeredPane.add(panel);
		panel.setLayout(null);

		JPanel panel_1 = new JPanel();
		panel_1.setBackground(new Color(245, 245, 220));
		panel_1.setBounds(496, 264, 161, 150);
		layeredPane.add(panel_1);
		layeredPane.setLayer(panel_1, 2);
		panel_1.setLayout(null);

		/*
		 * 回答
		 */
		JScrollPane catJScrollPane = new JScrollPane();
		layeredPane.setLayer(catJScrollPane, 11);
		catJScrollPane.setBounds(95, 216, 324, 47);
		layeredPane.add(catJScrollPane);
		JTextArea cat = new JTextArea();
		cat.setEditable(false);
		cat.setFont(new Font("幼圆", Font.BOLD, 14));
		catJScrollPane.setViewportView(cat);
		cat.setLineWrap(true);
		/*
		 * 提问
		 */
		JScrollPane myJScrollPanescrollPane = new JScrollPane();
		layeredPane.setLayer(myJScrollPanescrollPane, 11);
		myJScrollPanescrollPane.setBounds(195, 80, 305, 47);
		layeredPane.add(myJScrollPanescrollPane);
		JTextArea my = new JTextArea();
		my.setEditable(false);
		my.setBackground(new Color(143, 188, 143));
		my.setForeground(Color.WHITE);
		my.setFont(new Font("幼圆", Font.BOLD, 14));
		myJScrollPanescrollPane.setViewportView(my);
		my.setLineWrap(true);

		sendMsg = new JTextField();
		sendMsg.addKeyListener(new KeyAdapter() {// 按键监听事件
			@Override
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_ENTER) {
					String text = sendMsg.getText();// 获取消息输入框输入的消息
					sendMsg.setText("");// 重置输入框
					MessageContexts.getInstance().offerMessage(
							new MessageObj() {

								@Override
								public void handlerMessage(MyHttp info) {
									Object data = info.getData();
									String string = (String) data;
									cat.setText("");
									cat.append("\n" + string);
									showMsg.setCaretPosition(showMsg
											.getDocument().getLength());// 重新定位焦点保证新加的文字能显示出来
									showMsg.append("虹小猫：" + string + "\n");
									showMsg.setCaretPosition(showMsg
											.getDocument().getLength());// 重新定位焦点保证新加的文字能显示出来

									Speak.getInstance().shuo(string);

								}

								@Override
								public MyHttp getMessage() {// 对不同的用户输入利用工厂获取不同的消息类型
									MyHttp getmyHttp = null;

									showMsg.append("我：" + text + "\n");
									showMsg.setCaretPosition(showMsg
											.getDocument().getLength());// 重新定位焦点保证新加的文字能显示出来
									getmyHttp = MyHttpFactory.getmyHttp(text);
									my.setText("");
									my.append("\n" + text);
									showMsg.setCaretPosition(showMsg
											.getDocument().getLength());// 重新定位焦点保证新加的文字能显示出来
									if (getmyHttp.getMyUrl().equals("Plays")) {// 如果是玩游戏，显示游戏界
										layeredPane.setLayer(panel_1, 16);// 打开石头剪刀界面
									}

									return getmyHttp;
								}
							});

				}

			}
		});
		sendMsg.setBounds(19, 448, 372, 39);
		layeredPane.add(sendMsg);
		sendMsg.setColumns(10);

		/*
		 * 语音波纹
		 */
		JLabel lblNewLabel = new JLabel("");
		layeredPane.setLayer(lblNewLabel, 14);
		lblNewLabel.setBounds(176, 104, 191, 186);
		layeredPane.add(lblNewLabel);
		/*
		 * 文字发送
		 */
		ImageIcon icon2 = new ImageIcon("res/send.png");
		JButton writeButton = new JButton(icon2);
		writeButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));// 放上去变小手
		writeButton.addActionListener(new myActionlistener(sendMsg,
				layeredPane, panel, panel_1, showMsg, my, cat, true,
				lblNewLabel));
		layeredPane.setLayer(writeButton, 11);
		writeButton.setBounds(416, 398, 51, 47);
		writeButton.setMargin(new Insets(0, 0, 0, 0));// 将边框外的上下左右空间设置为0
		writeButton.setIconTextGap(0);// 将标签中显示的文本和图标之间的间隔量设置为0
		writeButton.setBorderPainted(false);// 不打印边框
		writeButton.setBorder(null);// 除去边框
		writeButton.setText(null);// 除去按钮的默认名称
		writeButton.setFocusPainted(false);// 除去焦点的框
		writeButton.setContentAreaFilled(false);// 除去默认的背景填充
		layeredPane.add(writeButton);

		JLabel lblNewLabel_3 = new JLabel("New label");
		lblNewLabel_3.setIcon(new ImageIcon("res/my.png"));

		/*
		 * 语音输入
		 */
		ImageIcon icon = new ImageIcon("res/speech.png");
		JButton speechButton = new JButton(icon);
		/*
		 * 鼠标按下,松开事件
		 */
		speechButton.addMouseListener(new myMouseListener(sendMsg, layeredPane,
				panel, panel_1, showMsg, my, cat, lblNewLabel));

		speechButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));// 放上去变小手
		// speechButton.addActionListener(new myActionlistener(sendMsg,
		// layeredPane, panel, panel_1, showMsg, my, cat, false,
		// lblNewLabel));
		lblNewLabel_3.setBounds(90, 47, 71, 83);
		panel.add(lblNewLabel_3);
		speechButton.setBounds(405, 448, 77, 39);
		speechButton.setMargin(new Insets(0, 0, 0, 0));// 将边框外的上下左右空间设置为0
		speechButton.setIconTextGap(0);// 将标签中显示的文本和图标之间的间隔量设置为0
		speechButton.setBorderPainted(false);// 不打印边框
		speechButton.setBorder(null);// 除去边框
		speechButton.setText(null);// 除去按钮的默认名称
		speechButton.setFocusPainted(false);// 除去焦点的框
		speechButton.setContentAreaFilled(false);// 除去默认的背景填充
		layeredPane.add(speechButton);
		/*
		 * 石头：1 剪刀：2 布：3
		 */
		ImageIcon iconShiTou = new ImageIcon("res/shitou.png");
		JButton shiTou = new JButton(iconShiTou);
		shiTou.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));// 放上去变小手
		shiTou.addActionListener(new GameListener(sendMsg, showMsg, "1", my,
				cat));
		shiTou.setBounds(57, 106, 45, 44);
		shiTou.setMargin(new Insets(0, 0, 0, 0));// 将边框外的上下左右空间设置为0
		shiTou.setIconTextGap(0);// 将标签中显示的文本和图标之间的间隔量设置为0
		shiTou.setBorderPainted(false);// 不打印边框
		shiTou.setBorder(null);// 除去边框
		shiTou.setText(null);// 除去按钮的默认名称
		shiTou.setFocusPainted(false);// 除去焦点的框
		shiTou.setContentAreaFilled(false);// 除去默认的背景填充
		panel_1.add(shiTou);

		/*
		 * 剪刀
		 */
		ImageIcon iconJiandao = new ImageIcon("res/jiandao.png");
		JButton jianDao = new JButton(iconJiandao);
		jianDao.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));// 放上去变小手
		jianDao.addActionListener(new GameListener(sendMsg, showMsg, "2", my,
				cat));
		jianDao.setBounds(57, 0, 45, 44);
		jianDao.setMargin(new Insets(0, 0, 0, 0));// 将边框外的上下左右空间设置为0
		jianDao.setIconTextGap(0);// 将标签中显示的文本和图标之间的间隔量设置为0
		jianDao.setBorderPainted(false);// 不打印边框
		jianDao.setBorder(null);// 除去边框
		jianDao.setText(null);// 除去按钮的默认名称
		jianDao.setFocusPainted(false);// 除去焦点的框
		jianDao.setContentAreaFilled(false);// 除去默认的背景填充
		panel_1.add(jianDao);
		/*
		 * 布
		 */
		ImageIcon iconBu = new ImageIcon("res/bu.png");
		JButton bu = new JButton(iconBu);
		bu.setBounds(57, 59, 45, 44);
		panel_1.add(bu);
		bu.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));// 放上去变小手
		bu.addActionListener(new GameListener(sendMsg, showMsg, "3", my, cat));
		bu.setMargin(new Insets(0, 0, 0, 0));// 将边框外的上下左右空间设置为0
		bu.setIconTextGap(0);// 将标签中显示的文本和图标之间的间隔量设置为0
		bu.setBorderPainted(false);// 不打印边框
		bu.setBorder(null);// 除去边框
		bu.setText(null);// 除去按钮的默认名称
		bu.setFocusPainted(false);// 除去焦点的框
		bu.setContentAreaFilled(false);

		JLabel label = new JLabel("New label");
		layeredPane.setLayer(label, 2);
		label.setBounds(24, 349, 101, 95);
		layeredPane.add(label);
		ImageIcon image2 = new ImageIcon("res/send2.png");
		Image img2 = image2.getImage();
		img2 = img2.getScaledInstance(101, 95, Image.SCALE_DEFAULT);
		image2.setImage(img2);
		label.setIcon(image2);

		JLabel lblNewLabel_1 = new JLabel("New label");
		layeredPane.setLayer(lblNewLabel_1, 3);
		lblNewLabel_1.setIcon(new ImageIcon("res/send3.png"));
		lblNewLabel_1.setBounds(64, 405, 239, 50);
		layeredPane.add(lblNewLabel_1);
		/*
		 * 打开
		 */
		ImageIcon imageIcon = new ImageIcon("res/open.png");
		JButton openHistoryButton = new JButton(imageIcon);
		openHistoryButton.setCursor(Cursor
				.getPredefinedCursor(Cursor.HAND_CURSOR));// 放上去变小手
		openHistoryButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				layeredPane.setLayer(scrollPane, 30);
			}
		});
		openHistoryButton.setBounds(538, 447, 33, 34);
		openHistoryButton.setMargin(new Insets(0, 0, 0, 0));// 将边框外的上下左右空间设置为0
		openHistoryButton.setIconTextGap(0);// 将标签中显示的文本和图标之间的间隔量设置为0
		openHistoryButton.setBorderPainted(false);// 不打印边框
		openHistoryButton.setBorder(null);// 除去边框
		openHistoryButton.setText(null);// 除去按钮的默认名称
		openHistoryButton.setFocusPainted(false);// 除去焦点的框
		openHistoryButton.setContentAreaFilled(false);// 除去默认的背景填充
		layeredPane.add(openHistoryButton);
		/*
		 * 关闭聊天记录
		 */
		ImageIcon imageIcon2 = new ImageIcon("res/close.png");
		JButton closeHistoryButton = new JButton(imageIcon2);
		closeHistoryButton.setCursor(Cursor
				.getPredefinedCursor(Cursor.HAND_CURSOR));// 放上去变小手
		closeHistoryButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				layeredPane.setLayer(scrollPane, 3);
				layeredPane.setLayer(panel_1, 3);// 关闭石头剪刀布界面
			}
		});
		closeHistoryButton.setBounds(603, 447, 33, 34);
		closeHistoryButton.setMargin(new Insets(0, 0, 0, 0));// 将边框外的上下左右空间设置为0
		closeHistoryButton.setIconTextGap(0);// 将标签中显示的文本和图标之间的间隔量设置为0
		closeHistoryButton.setBorderPainted(false);// 不打印边框
		closeHistoryButton.setBorder(null);// 除去边框
		closeHistoryButton.setText(null);// 除去按钮的默认名称
		closeHistoryButton.setFocusPainted(false);// 除去焦点的框
		closeHistoryButton.setContentAreaFilled(false);// 除去默认的背景填充
		layeredPane.add(closeHistoryButton);

		JLabel lblNewLabel_2 = new JLabel("New label");
		lblNewLabel_2.setIcon(new ImageIcon("res/cat.png"));
		lblNewLabel_2.setBounds(19, 194, 62, 67);
		layeredPane.add(lblNewLabel_2);

	}
}